package DataModel;

import MySqlAccess.RatingInfo;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author tuyetphuong
 */
public class ArrDataModel {

    public int numofrow; 
    public List<DataModel> arrmodel = new ArrayList<DataModel>(numofrow);
    public  int _NumOfUser;
    public  int _NumOfItem;
    public  int _MaxUser;
    public  int _MaxItem;

    public ArrDataModel() {
    }

    public ArrDataModel(int _numofrow, DataModel[] _arrmodel) {
        numofrow = _numofrow;
        arrmodel = new ArrayList<DataModel>(_numofrow);
        for (int i = 0; i < numofrow; i++) {
            arrmodel.add(_arrmodel[i]);
        }
    }

    public int GetNumOfRow() {
        return numofrow;
    }

    public int GetMaxUser() {
        int max = 0;
        for (int i = 0; i < numofrow; i++) {
            if (max < arrmodel.get(i).GetIDUser()) {
                max = arrmodel.get(i).GetIDUser();
            }
        }
        _MaxUser = max;
        return max;
    }

    public int GetMaxItem() {
        int max = 0;
        for (int i = 0; i < numofrow; i++) {
            if (max < arrmodel.get(i).GetIDItem()) {
                max = arrmodel.get(i).GetIDItem();
            }
        }
        _MaxItem = max;
        return max;
    }

    public int GetNumOFUser() {
        int count = 0;
        _MaxUser = GetMaxUser();
        int[] flag = new int[_MaxUser + 1];
        for (int i = 1; i <= _MaxUser; i++) {
            flag[i] = 0;
        }
        for (int i = 0; i < GetNumOfRow(); i++) {
            int a = arrmodel.get(i).iIDUser;
            flag[a] = 1;
        }
        for (int i = 1; i <= _MaxUser; i++) {
            if (flag[i] == 1) {
                count++;
            }
        }
        _NumOfUser = count;
        return count;
    }

    public int GetNumOFItem() {

        _MaxItem = GetMaxItem();
        int count = 0;
        int[] flag = new int[_MaxItem + 1];
        for (int i = 1; i <= _MaxItem; i++) {
            flag[i] = 0;
        }
        for (int i = 0; i < GetNumOfRow(); i++) {
            int a = arrmodel.get(i).GetIDItem();
            flag[a] = 1;
        }
        for (int i = 1; i <= _MaxItem; i++) {
            if (flag[i] == 1) {
                count++;
            }
        }
        _NumOfItem = count;
        return count;

    }

    public int[] GetItemSet() {
        int[] Item = new int[_NumOfItem];
        int count = 0;
        int[] flag = new int[_MaxItem + 1];
        for (int i = 1; i <= _MaxItem; i++) {
            flag[i] = 0;
        }
        for (int i = 0; i < GetNumOfRow(); i++) {
            int a = arrmodel.get(i).GetIDItem();
            flag[a] = 1;
        }
        for (int i = 1; i <= _MaxItem; i++) {
            if (flag[i] == 1) {
                Item[count] = i;
                count++;
            }

        }
        return Item;
    }
     public int[] GetUserSet() {
        int[] User = new int[_NumOfUser];
        int count = 0;
        int[] flag = new int[_MaxUser + 1];
        for (int i = 1; i <= _MaxUser; i++) {
            flag[i] = 0;
        }
        for (int i = 0; i < GetNumOfRow(); i++) {
            int a = arrmodel.get(i).GetIDUser();
            flag[a] = 1;
        }
        for (int i = 1; i <= _MaxUser; i++) {
            if (flag[i] == 1) {
                User[count] = i;
                count++;
            }
        }
        return User;
    }

    public int[] CountIDUser() {
        int a = _MaxUser+1000;
        int Count[] = new int[a + 1];
        for (int i = 1; i <= a; i++) {
            Count[i] = 0;
        }
        for (int i = 0; i < numofrow; i++) {
            Count[arrmodel.get(i).GetIDUser()]++;
        }
        return Count;
    }

    public int[] FirstIndexUser() {
        int a = _MaxUser+1000;

        int Index[] = new int[a + 1];
        int Flag[] = new int[a + 1];

        for (int i = 1; i <= a; i++) {
            Index[i] = 0;
            Flag[i] = 0;
        }
        for (int i = 0; i < numofrow; i++) {

            if (Flag[arrmodel.get(i).GetIDUser()] == 0) {
                Flag[arrmodel.get(i).GetIDUser()] = 1;
                Index[arrmodel.get(i).GetIDUser()] = i;

            }

        }
        return Index;
    }

    public int[] FindItemLastUse(int iuser) {
        int NumOfItem = 0;
        int iItemSet[] = new int[NumOfItem];
        int a = numofrow;
        int FirstIndex[] = FirstIndexUser();
        int start = FirstIndex[iuser];
        int Count[] = CountIDUser();
        int end = start + Count[iuser];
        for (int i = start; i < end; i++) {
            if (arrmodel.get(i).GetIDUser() == iuser && arrmodel.get(i).iRating==1) {

                iItemSet = new int[NumOfItem + 1];
                iItemSet[NumOfItem] = arrmodel.get(i).GetIDItem();
                NumOfItem++;

            }
        }
        return iItemSet;
    }
     public MaTrixUser_Item CreateMatrixUser_Item() {
        MaTrixUser_Item matrix = new MaTrixUser_Item();

        matrix.numofuser = _MaxUser;
        matrix.arr_user_item = new USer_Item[matrix.numofuser + 1];
        int count = 0;
        for (int i = 1; i <= _MaxUser; i++) {
            USer_Item ui = new USer_Item();
            USer_Item ui2 = new USer_Item();
            int[] Index = FirstIndexUser();
            int start = Index[i];
            count = 0;
            int Count[] = CountIDUser();
            int end = Count[i];
            ui.user = i;
            ui.ITem = new int[end];
            for (int j = start; j < start + end; j++) {
                if (arrmodel.get(j).GetRating() >= 1) {
                    ui.ITem[count] = arrmodel.get(j).GetIDItem();
                    count++;
                }
            }
            ui2.ITem = new int[count];
            ui2.user = ui.user;
            for (int k = 0; k < count; k++) {
                ui2.ITem[k] = ui.ITem[k];
            }
            matrix.arr_user_item[i] = ui2;
        }
        matrix = new MaTrixUser_Item(matrix.numofuser, matrix.arr_user_item);
        return matrix;


    }

    public void Add (RatingInfo rating)
    {
        DataModel datamodel = new DataModel(rating.m_idUser,
                rating.m_idItem, rating.m_rating, rating.m_dateTime.getTime());
        arrmodel.add(datamodel);
    }
     public int[] UserRating(int user) {
        int[] Item = new int[10000];
        int numofitem = 0;
        for (int i = 0; i <numofrow; i++) {
            if ((arrmodel.get(i).GetRating() >= 1) && (arrmodel.get(i).GetIDUser() == user)) {
                Item[numofitem] = arrmodel.get(i).GetIDItem();
                numofitem++;
            }
        }
        int[] ITem2 = new int[numofitem];
        for (int i = 0; i < numofitem; i++) {
            ITem2[i] = Item[i];
        }
        return ITem2;
    }

    public int[] UserRating(int user, int time) {
        int[] Item = new int[10000];
        int numofitem = 0;
        int[] First = FirstIndexUser();
        int[] Count = CountIDUser();
        for (int i = First[user]; i < First[user] + Count[user]; i++) {
            if ((arrmodel.get(i).GetRating() >= 3) && (arrmodel.get(i).GetIDUser() == user) &&
                    (arrmodel.get(i).GetTimeStamp() >= time)) {
                Item[numofitem] = arrmodel.get(i).GetIDItem();
                numofitem++;
            }
        }

        int[] ITem2 = new int[numofitem];
        for (int i = 0; i < numofitem; i++) {
            ITem2[i] = Item[i];
        }

        return ITem2;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    }
}
